<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证" prop="certno">
        <el-input
          v-model="queryParams.certno"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名赛事" prop="competitionId">
          <el-select v-model="queryParams.competitionId" placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        </el-form-item>
        <!--        <el-input-->
        <!--          v-model="queryParams.competitionId"-->
        <!--          placeholder="请输入报名赛事"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
      <el-form-item label="考场编号" prop="examAddress">
        <el-input
          v-model="queryParams.examAddress"
          placeholder="请输入考场编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="座位号" prop="examSeatnumber">
        <el-input
          v-model="queryParams.examSeatnumber"
          placeholder="请输入考场座位号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:exam:add']"
        >分配考场</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:exam:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:exam:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:exam:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  />
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号码" align="center" prop="certno" />
      <el-table-column label="考场编号" align="center" prop="examAddress" />
      <el-table-column label="考场座位号" align="center" prop="examSeatnumber" />
      <el-table-column label="赛事名称" align="center" prop="competitionId" width="325" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:exam:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:exam:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加成绩管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="赛事" prop="competitionId">
          <el-select v-model="form.competitionId" placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        </el-form-item>
        <el-form-item label="考场总数" prop="examAddress">
          <el-input v-model="form.examAddress" placeholder="请输入考场号" />
        </el-form-item>
        <el-form-item label="考场座位号" prop="examSeatnumber">
          <el-input v-model="form.examSeatnumber" placeholder="请输入考场座位号" />
        </el-form-item>
        <el-row>总座位数 = 考场总数 x 考场座位号 </el-row>
        <el-row>请保证: 总座位数 >= 赛事参加总人数</el-row>
        <el-row>建议: 可多预备一些座位或考场</el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改成绩管理对话框 -->
    <el-dialog :title="title" :visible.sync="update" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules1" label-width="120px">
        <el-form-item label="赛事" prop="competitionId">
          <el-select v-model="form.competitionId" disabled placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        </el-form-item>
        <el-form-item label="考场总数" prop="examAddress">
          <el-input v-model="form.examAddress" placeholder="请输入考场号" />
        </el-form-item>
        <el-form-item label="考场座位号" prop="examSeatnumber">
          <el-input v-model="form.examSeatnumber" placeholder="请输入考场座位号" />
        </el-form-item>
        <el-row>注:修改考场和座位号信息需要保证不重复</el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {addExam, delExam, getExam, listExam, updateExam} from "@/api/exam/exam";
import {loadCompetition} from "@/api/student/student";

export default {
  name: "Exam",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成绩管理表格数据
      examList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      update: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certno: null,
        competitionId: null,
        examAddress: null,
        examSeatnumber: null,
        examScores: null,
        examReview: null,
        examReviewtime: null,
        examReviewBy: null,
        examStatus: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        competitionId: [
          { required: true, message: "赛事名称不能为空", trigger: "change" }
        ],
        examSeatnumber: [
          { required: true, message: "考场信息不能为空", trigger: "change" }
        ],
        examAddress: [
          { required: true, message: "座位号不能为空", trigger: "change" }
        ],
        examReview: [
          { required: true, message: "成绩审核不能为空", trigger: "change" }
        ],
      },
      rules1: {
        competitionId: [
          { required: true, message: "赛事名称不能为空", trigger: "change" }
        ],
        examSeatnumber: [
          { required: true, message: "座位号不能为空", trigger: "change" },
          { pattern: /^[\s\S]{1,10}座位$/, message: '请输入正确的格式: XX座位'}
        ],
        examAddress: [
          { required: true, message: "考场号不能为空", trigger: "change" },
          { pattern: /^[\s\S]{1,10}号考场$/, message: '请输入正确的格式: XX号考场'}
        ],
        examReview: [
          { required: true, message: "成绩审核不能为空", trigger: "change" }
        ],
      },
      options: [],
    };
  },
created() {
    this.getList();
    this.loadCompetition();
  },
  methods: {
    loadCompetition() {
      console.log("赛事下拉选加载");
      loadCompetition().then(response => {
        this.options = response.data
        console.log(this.options);
      });
    },
    /** 查询成绩管理列表 */
    getList() {
      this.loading = true;
      listExam(this.queryParams).then(response => {
        this.examList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.update = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        certno: null,
        competitionId: null,
        examAddress: null,
        examSeatnumber: null,
        examScores: null,
        examReview: null,
        examReviewtime: null,
        examReviewBy: null,
        examStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "考场分配";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExam(id).then(response => {
        this.form = response.data;
        this.form.competitionId = Number(this.form.competitionId);
        this.update = true;
        this.title = "修改成绩管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.update = false;
              this.getList();
            });
          } else {
            this.open = false;
            this.update = false;
            this.$message({
              message: '考场自动分配中!请稍等!',
              type: 'success'
            });
            addExam(this.form).then(response => {
              this.$modal.msgSuccess("自动分配成功!");
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认删除名称为"' + names + '"的数据项？').then(function() {
        return delExam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      if(!(this.queryParams.competitionId == null)){
        this.download('system/exam/export', {
          ...this.queryParams
        }, `考场安排明细_${new Date().toLocaleString()}.xlsx`)
      }else {
        this.$modal.msgError("请先进行赛事筛选!");
        return;
      }
    }
  }
};
</script>
